/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.tor.tribes.reportserver.ui;

import com.vaadin.data.Item;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;
import de.tor.tribes.reportserver.IReportInfoPanelListener;
import de.tor.tribes.reportserver.MyVaadinApplication;
import de.tor.tribes.reportserver.PersistenceImpl;
import de.tor.tribes.reportserver.util.UIUtil;
import de.tor.tribes.reportserver.util.VelocityReportRenderer;
import de.tor.tribes.types.ZebraReport;
import de.tor.tribes.types.ZebraReportAccessGrant;
import de.tor.tribes.types.ZebraReportGroup;
import de.tor.tribes.types.ZebraUser;
import java.util.List;

/**
 *
 * @author Torridity
 */
public class ReportInfoPanel extends CustomComponent {

    private GridLayout mainLayout;
    private CheckBox colorLabel;
    private TextField reportTitle;
    private ComboBox reportGroupBox;
    private NativeButton changeGroupButton;
    private NativeButton changeTitleButton;
    private NativeButton deleteReportButton;
    private NativeButton deleteReportGroupButton;
    private NativeButton shareReportButton;
    private NativeButton viewReportButton;
    private ZebraReportAccessGrant report;
    private IReportInfoPanelListener listener;
    private PersistenceImpl persistence;

    public ReportInfoPanel(ZebraReportAccessGrant pReport, ZebraUser pUser, PersistenceImpl pPersistence, IReportInfoPanelListener pListener) {
        report = pReport;
        listener = pListener;
        persistence = pPersistence;
        buildMainLayout(pUser);
        setCompositionRoot(mainLayout);
    }

    private void buildMainLayout(final ZebraUser pUser) {
        //@TODO check ownership of report to allow/disallow changes
        ThemeResource okIcon = new ThemeResource("../zebra/img/save.png");
        ThemeResource deleteIcon = new ThemeResource("../zebra/img/delete.png");
        ThemeResource shareIcon = new ThemeResource("../zebra/img/share.png");
        ThemeResource viewIcon = new ThemeResource("../zebra/img/show.png");
        setHeight("45px");
        colorLabel = new CheckBox();
        colorLabel.setWidth("25px");
        colorLabel.setHeight("45px");
        reportTitle = new TextField();
        reportTitle.setWidth("100%");
        StringLengthValidator val = new StringLengthValidator("Der Berichttitel muss zwischen 3 und 255 Zeichen lang sein.");
        val.setMinLength(3);
        val.setMaxLength(255);
        reportTitle.addValidator(val);
        reportTitle.setValue(getReport().getTitle());
        reportTitle.setStyleName("zebrareport");

        changeTitleButton = new NativeButton();
        changeTitleButton.setDescription("Berichttitel ändern");
        changeTitleButton.setWidth("25px");
        changeTitleButton.setStyleName("zebrareport");
        changeTitleButton.setIcon(okIcon);
        changeTitleButton.addListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                if (reportTitle.isValid()) {
                    getReport().setTitle((String) reportTitle.getValue());
                    persistence.persist(report);
                    getApplication().getMainWindow().showNotification("Bericht umbenannt", "Der Bericht wurde erfolgreich umbenannt.", Window.Notification.TYPE_TRAY_NOTIFICATION);
                }
            }
        });

        changeGroupButton = new NativeButton();
        changeGroupButton.setWidth("25px");
        changeGroupButton.setDescription("Berichtgruppe ändern");
        changeGroupButton.setIcon(okIcon);
        changeGroupButton.setStyleName("zebrareport");

        changeGroupButton.addListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                Object value = reportGroupBox.getValue();
                if (value == null) {
                    return;
                }
                ZebraReportGroup selection;
                if (value instanceof Long) {
                    //existing id
                    selection = persistence.find((Long) value, ZebraReportGroup.class);
                } else {
                    selection = new ZebraReportGroup();
                    selection.setName((String) value);
                    selection.setOwner(pUser);
                    persistence.persist(selection);
                }
                listener.fireChangeReportGroupEvent(ReportInfoPanel.this, selection);
            }
        });

        deleteReportButton = new NativeButton();
        deleteReportButton.setWidth("25px");
        deleteReportButton.setDescription("Bericht löschen");
        deleteReportButton.setIcon(deleteIcon);
        deleteReportButton.setStyleName("zebrareport");

        deleteReportButton.addListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                listener.fireReportDeleteEvent(ReportInfoPanel.this);
            }
        });

        deleteReportGroupButton = new NativeButton();
        deleteReportGroupButton.setWidth("25px");
        deleteReportGroupButton.setDescription("Berichtgruppe und alle enthaltenen Berichte löschen");
        deleteReportGroupButton.setIcon(deleteIcon);
        deleteReportGroupButton.setStyleName("zebrareport");

        deleteReportGroupButton.addListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                Object value = reportGroupBox.getValue();
                if (value == null) {
                    return;
                }
                if (value instanceof Long) {
                    //existing id
                    ZebraReportGroup selection = persistence.find((Long) value, ZebraReportGroup.class);
                    listener.fireDeleteReportGroupEvent(ReportInfoPanel.this, selection);
                }

            }
        });

        shareReportButton = new NativeButton();
        shareReportButton.setWidth("25px");
        shareReportButton.setDescription("Bericht für andere Nutzer freigeben");
        shareReportButton.setIcon(shareIcon);
        shareReportButton.setStyleName("zebrareport");
        shareReportButton.addListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                listener.fireReportShareEvent(ReportInfoPanel.this);
            }
        });

        viewReportButton = new NativeButton();
        viewReportButton.setWidth("25px");
        viewReportButton.setDescription("Bericht für andere Nutzer freigeben");
        viewReportButton.setIcon(viewIcon);
        viewReportButton.setStyleName("zebrareport");
        viewReportButton.addListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                VelocityReportRenderer renderer = null;
                try {
                    renderer = new VelocityReportRenderer(((MyVaadinApplication) getApplication()).getPersistence(),((MyVaadinApplication) getApplication()).getVelocity());
                    
                    
                    String renderedReport = renderer.renderReport(report);

                    Window w = new Window(report.getReport().getTitle());
                    Label content = new Label(renderedReport, Label.CONTENT_XHTML);
                    Panel p = new Panel();
                    p.addComponent(content);
                    w.setContent(p);
                    getApplication().getMainWindow().addWindow(w);
                    w.center();
                } catch (Exception e) {
                    e.printStackTrace();
                    getApplication().getMainWindow().showNotification("Fehler", "Bericht kann nicht dargestellt werden.", Window.Notification.TYPE_ERROR_MESSAGE);
                }
            }
        });


        reportGroupBox = new ComboBox();
        reportGroupBox.setWidth("100%");
        reportGroupBox.setInputPrompt("Neue Gruppe eingeben");
        reportGroupBox.setNewItemsAllowed(true);
        reportGroupBox.setStyleName("zebrareport");
        reportGroupBox.addContainerProperty("name", String.class, null);
        reportGroupBox.setItemCaptionPropertyId("name");
        List<ZebraReportGroup> groups = persistence.getReportGroups(pUser);
        for (ZebraReportGroup group : groups) {
            Item item = reportGroupBox.addItem(group.getId());
            item.getItemProperty("name").setValue(group.getName());
        }
        if (getReportAccessGrant().getGroupMapping() != null) {
            reportGroupBox.select(getReportAccessGrant().getGroupMapping().getGroup().getId());
        }

        switch (getReport().getColor()) {
            case BLUE:
                colorLabel.setStyleName("blue");
                break;
            case GREEN:
                colorLabel.setStyleName("green");
                break;
            case YELLOW:
                colorLabel.setStyleName("yellow");
                break;
            case RED:
                colorLabel.setStyleName("red");
                break;
            case RED_BLUE:
                colorLabel.setStyleName("redblue");
                break;
            case RED_YELLOW:
                colorLabel.setStyleName("redyellow");
                break;
            default:
                colorLabel.setStyleName("unknown");

        }
        mainLayout = new UIUtil.GridLayoutBuilder(6, 2).
                addComponent(colorLabel, 0, 0, 1, 2).
                addComponent(reportTitle, 1, 0, 1, 1).
                addComponent(changeTitleButton, 2, 0, 1, 1).
                addComponent(viewReportButton, 3, 0, 1, 1).
                addComponent(shareReportButton, 4, 0, 1, 1).
                addComponent(deleteReportButton, 5, 0, 1, 1).
                addComponent(reportGroupBox, 1, 1, 3, 1).
                addComponent(changeGroupButton, 4, 1, 1, 1).
                addComponent(deleteReportGroupButton, 5, 1, 1, 1).getLayout();
        mainLayout.setComponentAlignment(colorLabel, Alignment.MIDDLE_CENTER);

        mainLayout.setColumnExpandRatio(1, 1f);
        mainLayout.setStyleName("zebrareport");
        mainLayout.setSpacing(false);
        mainLayout.setSizeFull();
        mainLayout.setHeight("45px");

        changeTitleButton.setVisible(report.isOwner());
        shareReportButton.setVisible(report.isOwner());
    }

    public ZebraReport getReport() {
        return report.getReport();
    }

    public ZebraReportAccessGrant getReportAccessGrant() {
        return report;
    }

    public boolean isSelected() {
        return colorLabel.booleanValue();
    }

    public void setSelected(boolean pValue) {
        colorLabel.setValue(pValue);
    }
}
